package com.peng.linkedlist;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class LinkedListUtils {

    public static Node link(Node... nodes) {
        return link(Arrays.asList(nodes));
    }

    public static Node link(List<Node> nodes) {
        for (int i = 0; i < nodes.size(); i++) {
            nodes.get(i).next = i + 1 < nodes.size() ? nodes.get(i + 1) : null;
        }

        return nodes.get(0);
    }

    public static Node makeLinkedList(int size) {
        Node head = new Node(1);
        Node cur = head;

        for (int i = 2; i <= size; i++) {
            cur.next = new Node(i);
            cur = cur.next;
        }

        return head;
    }

    public static Node makeRandomLinkedList(int size) {
        Random random = new Random();
        Node head = new Node(random.nextInt(1000));
        Node cur = head;

        for (int i = 2; i <= size; i++) {
            cur.next = new Node(random.nextInt(1000));
            cur = cur.next;
        }

        return head;
    }

    public static void printNodes(Node head) {
        StringBuilder sb = new StringBuilder("[");
        while (head != null) {
            sb.append(head.value + (head.next == null ? "]" : ","));
            head = head.next;
        }

        System.out.println(sb);
    }
}
